package test.controller;

import test.mapper.ArticleMapper;
import test.util.InputUtil;
import test.vo.Article;

import java.util.List;
import java.util.Scanner;
/**
 * 该类用于通过用户获取信息来对文章进行相关操作
 */
public class ArticleController {
    private static ArticleMapper articleMapper = new ArticleMapper();

    public static void main(String[] args) {
        ArticleController articleController = new ArticleController();
        articleController.start();
    }

    public void start(){
        while (true){
            System.out.println("欢迎来到文章管理系统");
            System.out.println("请输入操作编号");
            System.out.println("1：保存文章");
            System.out.println("2：删除文章");
            System.out.println("3：查询指定文章");
            System.out.println("4：查询所有文章");
            System.out.println("0：退出");
            Scanner scanner = new Scanner(System.in);
            String number = scanner.nextLine();
            switch (number){
                case "1":
                    //System.out.println("保存文章");
                    writeArticle();
                    break;
                case "2":
                    //System.out.println("删除文章");
                    removeArticle();
                    break;
                case "3":
                    //System.out.println("查询指定文章");
                    findArticleById();
                    break;
                case "4":
                    //System.out.println("查询所有文章");
                    findAll();
                    break;
                case "0":
                    System.out.println("再见");
                    return;
                    //System.exit(0);
                default:
                    System.out.println("输入错误");
            }
        }
    }

    //发表文章功能
    public static void writeArticle(){
        Article article = InputUtil.getInputObject(new Article(),"发表文章");
        int num = articleMapper.insertArticle(article);
        if (num > 0){
            System.out.println("发表成功");
        }else {
            System.out.println("发表失败");
        }
    }

    public static void removeArticle(){
        System.out.println("删除文章");
        System.out.println("请输入要删除的文章编号");
        Scanner sc = new Scanner(System.in);
        int id = Integer.parseInt(sc.nextLine());
        int num = articleMapper.deleteArticleById(id);
            System.out.println(num > 0?"删除成功":"删除失败");
    }

    public static void findArticleById(){
        System.out.println("查询指定文章");
        System.out.println("请输入要查询的文章编号");
        Scanner sc = new Scanner(System.in);
        int id = Integer.parseInt(sc.nextLine());
        Article article = articleMapper.findById(id);
        System.out.println("标题："+article.getTitle());
        System.out.println("正文："+article.getContent());
    }

    public static void findAll(){
        System.out.println("查询所有文章");
        List<Article> articleList = articleMapper.findAll();
        for (Article article : articleList){
            System.out.println("标题："+article.getTitle());
            System.out.println("正文："+article.getContent());
            System.out.println("-----------------------------------");
        }
    }
}
